package com.example.springboot2022nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot2022nocv.entity.Approval;
import com.example.springboot2022nocv.entity.Role;
import com.example.springboot2022nocv.entity.User;
import com.example.springboot2022nocv.enums.ApprovalNodeStatusEnum;
import com.example.springboot2022nocv.service.ApprovalService;
import com.example.springboot2022nocv.service.RoleService;
import com.example.springboot2022nocv.vo.ApprovalVo;
import com.example.springboot2022nocv.vo.DataView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2022/12/31 22:30
 * @title
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/approval")
public class ApprovalController {

    @Autowired
    private ApprovalService approvalService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/toApproval")
    public String toApproval() {
        return "approval/approval";
    }

    @RequestMapping("/loadAllApproval")
    @ResponseBody
    public DataView loadAllApproval(ApprovalVo approvalVo, HttpSession session) {
        //1.取到
        User user = (User) session.getAttribute("user");
        if (StringUtils.isNotEmpty(user.getUsername())) {
            String username = user.getUsername();
            Integer uid = user.getId();
            IPage<Approval> page = new Page<>(approvalVo.getPage(), approvalVo.getLimit());
            QueryWrapper<Approval> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(String.valueOf(uid)), "uid", uid);
            approvalService.page(page, queryWrapper);
            // 处理用户名
            List<Approval> records = page.getRecords();
            for (Approval a : records) {
                a.setUsername(username);
            }
            return new DataView(page.getTotal(), page.getRecords());
        }
        return new DataView();


    }

    @RequestMapping("/addApproval")
    @ResponseBody
    public DataView addApproval(Approval approval, HttpSession session) {
        approval.setCreateTime(new Date());
        // 未提交状态没有了
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        approval.setUid(id);
        // 多种角色 最好每一个人有一种角色
        List<Integer> role = roleService.queryUserRoleById(id);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        if (StringUtils.equals(roleName, "admin") || StringUtils.equals(roleName, "学生")) {
            approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_TEACHER_ING.getCode());
        } else if (StringUtils.equals(roleName, "教师")) {
            approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_ING.getCode());
        } else if (StringUtils.equals(roleName, "院系")) {
            approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_PASSED.getCode());
        } else {
            approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_TEACHER_ING.getCode());
        }
        approvalService.save(approval);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("申请请假成功!");
        return dataView;
    }

    @RequestMapping("/successApproval")
    @ResponseBody
    public DataView successApproval(Approval approval,HttpSession session) {
        DataView dataView = new DataView();
        approval.setUpdateTime(new Date());
        // 1.首先判断数据库中的状态
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        String username = user.getUsername();
        approval.setUid(id);

        List<Integer> role = roleService.queryUserRoleById(id);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        if (StringUtils.equals(roleName, "学生")) {
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行审批!");
            return dataView;
        }

        // 2.查询节点状态
        Approval serviceById = approvalService.getById(approval.getId());
        Integer nodeStatus = serviceById.getNodeStatus();

        // 如果是审批中状态 才可以审批通过
        if((StringUtils.equals(String.valueOf(ApprovalNodeStatusEnum.APPROVAL_TEACHER_ING.getCode()), String.valueOf(nodeStatus)))
                || (StringUtils.equals(String.valueOf(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_ING.getCode()),String.valueOf(nodeStatus)))){
            if (StringUtils.equals(roleName, "admin")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_PASSED.getCode());
            } else if (StringUtils.equals(roleName, "教师")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_TEACHER_PASSED.getCode());
            } else if (StringUtils.equals(roleName, "院系")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_PASSED.getCode());
            } else {
                approval.setNodeStatus(ApprovalNodeStatusEnum.APPROVAL_TEACHER_PASSED.getCode());
            }

            // 修改库
            approvalService.updateById(approval);
            dataView.setCode(200);
            dataView.setMsg(username+":角色:"+roleName+"角色审批通过!");
            return dataView;
        }

        dataView.setCode(100);
        dataView.setMsg("此状态不能审批哦!");
        return dataView;
    }

    @RequestMapping("/rejectApproval")
    @ResponseBody
    public DataView rejectApprova(Approval approval,HttpSession session) {
        DataView dataView = new DataView();
        approval.setUpdateTime(new Date());
        // 1.首先判断数据库中的状态
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        String username = user.getUsername();
        approval.setUid(id);

        List<Integer> role = roleService.queryUserRoleById(id);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        if (StringUtils.equals(roleName, "学生")) {
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行驳回!");
            return dataView;
        }

        // 2.查询节点状态
        Approval serviceById = approvalService.getById(approval.getId());
        Integer nodeStatus = serviceById.getNodeStatus();

        // 如果是审批中状态 才可以审批通过
        if((StringUtils.equals(String.valueOf(ApprovalNodeStatusEnum.APPROVAL_TEACHER_ING.getCode()), String.valueOf(nodeStatus)))
                || (StringUtils.equals(String.valueOf(ApprovalNodeStatusEnum.APPROVAL_COLLEGE_ING.getCode()),String.valueOf(nodeStatus)))){
            if (StringUtils.equals(roleName, "admin")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.COLLEGE_REJECTED.getCode());
            } else if (StringUtils.equals(roleName, "教师")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.TEACHER_REJECTED.getCode());
            } else if (StringUtils.equals(roleName, "院系")) {
                approval.setNodeStatus(ApprovalNodeStatusEnum.COLLEGE_REJECTED.getCode());
            } else {
                approval.setNodeStatus(ApprovalNodeStatusEnum.TEACHER_REJECTED.getCode());
            }

            // 修改库
            approvalService.updateById(approval);
            dataView.setCode(200);
            dataView.setMsg(username+":角色:"+roleName+"驳回成功!");
            return dataView;
        }

        dataView.setCode(100);
        dataView.setMsg("此状态不能驳回哦!");
        return dataView;
    }
}
